var http = require('http'),
  url = require('url'),
  fs = require('fs'),
  util = require('util');

http.createServer(function(req, res) {
  var pathname = url.parse(req.url).pathname;
  var functionName = 'res' + pathname.substr(1, 1).toUpperCase() + pathname.substr(2);
  if (pathname === '/') {
    resDefault(res);
  } else if (pathname === '/favicon.ico') {
    return;
  } else {
    eval(functionName + '(res)');
  }
}).listen(3000, '127.0.0.1');
console.log('server start at http://localhost:3000');

function resIndex(res) {
  var readPath = __dirname + '/views/' + url.parse('index.html').pathname;
  var indexPage = fs.readFileSync(readPath);
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end(indexPage);
}

function resImg(res) {
  var readPath = __dirname + '/static/imgs/' + url.parse('logo.png').pathname;
  var imgPage = fs.readFileSync(readPath);
  res.writeHead(200, { 'Content-Type': 'image/png' });
  res.end(imgPage);
}

function resDefault(res) {
  res.writeHead(404, { 'Content-Type': 'text/plain' });
  res.end('can not find Page!');
}
